//global vars
var ptType;

//functions
function countHpi(){ 
	var boxes = $("#hpi").find("input[type=checkbox]");

	var checked = boxes.filter(":checked").length; 
	var hpiLevel = 0;
	if(checked > 0) {
		$("td.hpi_prob-foc").css("background-color", "red");
		$("td.hpi_expand-pf").css("background-color", "red");
		hpiLevel = 2;
	};
	if(checked > 3) {
		$("td.hpi_detailed").css("background-color", "red");
		$("td.hpi_comp").css("background-color", "red");
		hpiLevel = 4;
	};
	return hpiLevel; 
};

function countRos(){ 
	var boxes = $("#ros").find("input[type=checkbox]");
	var checked = boxes.filter(":checked").length; 
	var rosLevel = 0;
	if(checked >= 0) {
		$("td.ros_prob-foc").css("background-color", "red");
		rosLevel = 1;
	};
	if(checked >= 1) {
		$("td.ros_expand-pf").css("background-color", "red");
		rosLevel = 2;
	};
	if(checked > 1) {
		$("td.ros_detailed").css("background-color", "red");
		rosLevel = 3;
	};
	if(checked > 9) {
		$("td.ros_comp").css("background-color", "red");
		rosLevel = 4;
	};
	return rosLevel; 
};

function countPfsh(){ 
	var boxes = $("#pfsh").find("input[type=checkbox]");
	var checked = boxes.filter(":checked").length; 
	var pfshLevel = 0;
	if(checked >= 0) {
		$("td.pfsh_prob-foc").css("background-color", "red");
		$("td.pfsh_expand-pf").css("background-color", "red");
		pfshLevel = 2;
	};
	if(checked > 0) {
		$("td.pfsh_detailed").css("background-color", "red");
		pfshLevel = 3;
	};
	if(checked > 1 && ptType == "est") {
		//Established Patient
		$("td.pfsh_comp").css("background-color", "red");
		pfshLevel = 4;
	};
	if(checked > 2 && (ptType == "new" || ptType == "con")) {
		//New Patient or Consult
		$("td.pfsh_comp").css("background-color", "red");
		pfshLevel = 4;
	};
	return pfshLevel; 
};

function checkExamLevel(){
	// counts systems marked
	var systemsExamined = 0;
	var systems = $('#exam').find('div');
	systems.each(function(){
		var checkboxes = $(this).find('input[type=checkbox]');
		if(checkboxes.filter(":checked").length > 0){
			systemsExamined++;
		};
	});
	// return exam level based on systems selected
	if(systemsExamined >= 8) return 4;
	if(systemsExamined >= 5) return 3;
	if(systemsExamined >= 2) return 2;
	if(systemsExamined >= 1) return 1;
};

function checkHisLevel(){
	//count up all the history levels based on checkboxes
	var hpiLevel = countHpi();
	var rosLevel = countRos();
	var pfshLevel = countPfsh();
	var levels = [hpiLevel, rosLevel, pfshLevel];
	var min = levels[0];
	for(var i=1; i < levels.length; i++) {
		if(levels[i] < min) min = levels[i];
	}
	return min;
};

function checkRiskLevel(){
	//check for the highest risk level checked
	var rows = $('#mdm_risk').find('tbody > tr');
	var levels = [];
	levels.push(0);
	rows.each(
		function() {
			var checkboxes = $(this).find('input[type=checkbox]');
			if(($(this).attr('class') == 'high') && (checkboxes.filter(":checked").length > 0)) {
				levels.push(4);
			}
			if(($(this).attr('class') == 'mod') && (checkboxes.filter(":checked").length > 0)) {
				levels.push(3);
			}
			if(($(this).attr('class') == 'low') && (checkboxes.filter(":checked").length > 0)) {
				levels.push(2);
			}
			if(($(this).attr('class') == 'min') && (checkboxes.filter(":checked").length > 0)) {
				levels.push(1);
			}
		}
	);

	var max = levels[0];
	for(var i=1; i < levels.length; i++) {
		if(levels[i] > max) max = levels[i];
	}
	return max;
}

function checkDxMgmtLevel() {
	//include error checking for max level
	var dxMgmtLevel = 0;
	dxMgmtLevel = dxMgmtLevel + parseInt( $('#dxmgmt_1').val(), 10 );
	dxMgmtLevel = dxMgmtLevel + parseInt( $('#dxmgmt_2').val(), 10 );
	dxMgmtLevel = dxMgmtLevel + parseInt( $('#dxmgmt_3').val(), 10 );
	dxMgmtLevel = dxMgmtLevel + parseInt( $('#dxmgmt_4').val(), 10 );
	dxMgmtLevel = dxMgmtLevel + parseInt( $('#dxmgmt_5').val(), 10 );

	if(dxMgmtLevel >= 4) return 4;
	if(dxMgmtLevel >= 3) return 3;
	if(dxMgmtLevel >= 2) return 2;
	if(dxMgmtLevel >= 1) return 1;

}

function checkAmtCmpLevel() {
	//include error checking for max level
	var amtCmpLevel = 0;
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_1').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_2').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_3').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_4').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_5').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_6').val(), 10 );
	amtCmpLevel = amtCmpLevel + parseInt( $('#amtcmp_7').val(), 10 );

	if(amtCmpLevel >= 4) return 4;
	if(amtCmpLevel >= 3) return 3;
	if(amtCmpLevel >= 2) return 2;
	if(amtCmpLevel >= 1) return 1;

}

function checkMdmLevel(){
	var riskLevel = checkRiskLevel();
	var dxMgmtLevel = checkDxMgmtLevel();
	var amtCmpLevel = checkAmtCmpLevel();

	var mdmLevel = [riskLevel, amtCmpLevel, dxMgmtLevel];
	mdmLevel.sort();

	return mdmLevel[1];

}

function colorMdmLevel(){
	var level = checkMdmLevel();

	$("tr#final-mdm").children().css("background-color", "");
	if(level >= 1) $('.mdm_level-1').css('background-color', 'red');
	if(level >= 2) $('.mdm_level-2').css('background-color', 'red');
	if(level >= 3) $('.mdm_level-3').css('background-color', 'red');
	if(level >= 4) $('.mdm_level-4').css('background-color', 'red');
}

function colorHisLevel(){
	var level = checkHisLevel();	

	$("tr#final-his").children().css("background-color", "");
	if(level >= 1) $(".his_level-1").css("background-color", "red");
	if(level >= 2) $(".his_level-2").css("background-color", "red");
	if(level >= 3) $(".his_level-3").css("background-color", "red");
	if(level >= 4) $(".his_level-4").css("background-color", "red");
}

function colorExamLevel(){
	var level = checkExamLevel();

	$("tr#final-exam").children().css("background-color", "");
	if(level >= 1) $(".exam_level-1").css("background-color", "red");
	if(level >= 2) $(".exam_level-2").css("background-color", "red");
	if(level >= 3) $(".exam_level-3").css("background-color", "red");
	if(level >= 4) $(".exam_level-4").css("background-color", "red");
}

function evalLevels(){
	var mdmLevel = checkMdmLevel();
	var hisLevel = checkHisLevel();
	var examLevel = checkExamLevel();
	$("tr." + ptType + "-pt").children().css("background-color", "");

	if(ptType == "est") {
		// established patients only need 2 of 3
		var levels = [mdmLevel, hisLevel, examLevel];
		levels.sort();

		if(levels[1] >= 1) $("." + ptType + "_1").css("background-color", "red");
		if(levels[1] >= 2) $("." + ptType + "_2").css("background-color", "red");
		if(levels[1] >= 2) $("." + ptType + "_3").css("background-color", "red");
		if(levels[1] >= 3) $("." + ptType + "_4").css("background-color", "red");
		if(levels[1] >= 3) $("." + ptType + "_5").css("background-color", "red");
		if(levels[1] >= 4) $("." + ptType + "_6").css("background-color", "red");
		if(levels[1] >= 4) $("." + ptType + "_7").css("background-color", "red");

	} else {
		// new and consults need 3 of 3

		if(hisLevel <= examLevel && hisLevel <= mdmLevel){
			//color based on hisLevel
			if(hisLevel >= 1) $("." + ptType + "_1").css("background-color", "red");
			if(hisLevel >= 2) $("." + ptType + "_2").css("background-color", "red");
			if(hisLevel >= 2) $("." + ptType + "_3").css("background-color", "red");
			if(hisLevel >= 3) $("." + ptType + "_4").css("background-color", "red");
			if(hisLevel >= 3) $("." + ptType + "_5").css("background-color", "red");
			if(hisLevel >= 4) $("." + ptType + "_6").css("background-color", "red");
			if(hisLevel >= 4) $("." + ptType + "_7").css("background-color", "red");
		} else if (examLevel <= hisLevel && examLevel <= mdmLevel) {
			//color based on examLevel
			if(examLevel >= 1) $("." + ptType + "_1").css("background-color", "red");
			if(examLevel >= 2) $("." + ptType + "_2").css("background-color", "red");
			if(examLevel >= 2) $("." + ptType + "_3").css("background-color", "red");
			if(examLevel >= 3) $("." + ptType + "_4").css("background-color", "red");
			if(examLevel >= 3) $("." + ptType + "_5").css("background-color", "red");
			if(examLevel >= 4) $("." + ptType + "_6").css("background-color", "red");
			if(examLevel >= 4) $("." + ptType + "_7").css("background-color", "red");
		} else if (mdmLevel <= examLevel && mdmLevel <= examLevel) {
			//color based on mdmLevel
			if(mdmLevel >= 1) $("." + ptType + "_1").css("background-color", "red");
			if(mdmLevel >= 1) $("." + ptType + "_2").css("background-color", "red");
			if(mdmLevel >= 2) $("." + ptType + "_3").css("background-color", "red");
			if(mdmLevel >= 2) $("." + ptType + "_4").css("background-color", "red");
			if(mdmLevel >= 3) $("." + ptType + "_5").css("background-color", "red");
			if(mdmLevel >= 3) $("." + ptType + "_6").css("background-color", "red");
			if(mdmLevel >= 4) $("." + ptType + "_7").css("background-color", "red");
		} else {
			alert("what happened?");
		}
	}
}
